package LeetCode_101_200;

import java.util.Arrays;
import java.util.Comparator;

class Solution179 {
    public String largestNumber(int[] nums) {
        // 将整数数组转换为字符串数组
        String[] strs = new String[nums.length];
        for (int i = 0; i < nums.length; i++) {
            strs[i] = String.valueOf(nums[i]);
        }

        // 自定义比较器，比较拼接后的字符串大小
        Arrays.sort(strs, new Comparator<String>() {
            @Override
            public int compare(String a, String b) {
                return (b + a).compareTo(a + b);
            }
        });

        // 处理全零的情况
        if (strs[0].equals("0")) {
            return "0";
        }

        // 拼接所有字符串
        StringBuilder sb = new StringBuilder();
        for (String s : strs) {
            sb.append(s);
        }
        return sb.toString();
    }
}

public class LeetCode179_largestNumber {
    public static void main(String[] args) {
        Solution179 solution = new Solution179();
        int[] nums1 = new int[] {10,2};
        int[] nums2 = new int[] {3,30,34,5,9};
        System.out.println(Arrays.toString(nums1));
        System.out.println(solution.largestNumber(nums1));
        System.out.println(solution.largestNumber(nums2));
    }
}